class menu_logic :
    def __init__ ( self , mediator ) :
        self . _configured_rows = [ ]
        self . _active_row = 0
        self . _last_command_row = 0
        self . _first_command_row = 0
        self . _commands_by_row = { }
        self . _mediator = mediator
    def start ( self ) :
        self . _awake = False
    def show_menu ( self ) :
        self . _awake = True
        self . _row = 0
        self . _add_title ( )
        self . _first_command_row = self . _row
        self . _commands_by_row = { }
        self . _add_menu_items ( )
        self . _last_command_row = self . _row - 1
        self . _active_row = self . _first_command_row
        self . _select_active_row ( )
    def hide_menu ( self ) :
        self . _awake = False
        self . _configured_rows = [ ]
    def configure_title_text_for_menu ( self , title_text ) :
        self . _title_text = title_text
    def configure_next_row_for_menu ( self , row_text , row_callback ) :
        self . _configured_rows += [ 
            { 'text' : row_text
            , 'command' : row_callback 
            } ]
    def on_table_row_click ( self , row ) :
        if self . _awake :
            if row in self . _commands_by_row :
                self . _unselect_active_row ( )
                self . _active_row = row
                self . _select_active_row ( )
                self . _commands_by_row [ row ] ( )
    def on_key_down ( self ) :
        if self . _awake :
            self . _unselect_active_row ( )
            self . _active_row += 1
            if self . _active_row > self . _last_command_row :
                self . _active_row = self . _first_command_row
            self . _select_active_row ( )
    def on_key_up ( self ) :
        if self . _awake :
            self . _unselect_active_row ( )
            self . _active_row -= 1
            if self . _active_row < self . _first_command_row :
                self . _active_row = self . _last_command_row
            self . _select_active_row ( )
    def on_key_return ( self ) :
        if self . _awake :
            if self . _active_row in self . _commands_by_row :
                self . _commands_by_row [ self . _active_row ] ( )
    def _add_title ( self ) :
        self . _mediator . add_table_label ( text = self . _title_text )
        self . _mediator . finish_table_row ( )
        self . _row += 1
    def _add_menu_items ( self ) :
        for configured_row in self . _configured_rows :
            self . _add_menu_item \
                ( configured_row [ 'text' ] 
                , configured_row [ 'command' ]
                )
    def _add_menu_item ( self , text , command ) :
        self . _mediator . add_table_label ( text = text )
        self . _mediator . finish_table_row ( )
        self . _commands_by_row [ self . _row ] = command
        self . _row += 1
    def _select_active_row ( self ) :
        self . _mediator . select_table_row ( self . _active_row )
    def _unselect_active_row ( self ) :
        self . _mediator . unselect_table_row ( self . _active_row )
